/**
 * 
 */
package fx.parser;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sajia
 *
 */
public class ExcludeExpression extends BooleanExpression {
	// right hand side is a phrase to exclude
	// right associative
	private final BooleanExpression left;
	private final List<BooleanExpression> excludes;
	
	public ExcludeExpression(final BooleanExpression left, final BooleanExpression right) {
		this.left = left;
		this.excludes = new ArrayList<BooleanExpression>();
		if (right instanceof ExcludeExpression) {
			ExcludeExpression rightExcludeExpr = (ExcludeExpression) right;
			this.excludes.add(rightExcludeExpr.left);
			this.excludes.addAll(rightExcludeExpr.excludes);
		} else {
			this.excludes.add(right);
		}
	}
	
	@Override
	protected void matchesWith(final String input, final MatchResult result) {
		for (BooleanExpression expr : this.excludes) {
			expr.matchesWith(input, result);
			if (result.isDoneMatching()) return;
			boolean exclusionMatched = result.isMatched();
			if (exclusionMatched) {
				result.doneMatching();
				result.setMatched(false);
				return;
			}
		}
		
		this.left.matchesWith(input, result);
	}
}
